using Godot;
using System;
using System.Collections.Generic;

public partial class TalkBeforeLevel : Node2D
{
	[Signal]
	public delegate void OnTalkEndEventHandler();
	CanvasLayer canvasLayer;
	TextureRect actorAvatar;
	Label actorName;
	Label actorWord;
	Panel panelMask;

	List<TalkModel> talkWords;
	int talkIndex = 0;
	TalkModel talkStory;

	void GetChildNodes()
	{
		canvasLayer = GetNode<CanvasLayer>("CanvasLayer");
		actorAvatar = GetNode<TextureRect>("%ActorAvatar");
		actorName = GetNode<Label>("%ActorName");
		actorWord = GetNode<Label>("%ActorWord");
		panelMask = GetNode<Panel>("%PanelMask");

		panelMask.GuiInput += OnPanelMaskInputHandler;
		AudioManager.PlayBgm("start_map");
	}

	private void OnPanelMaskInputHandler(InputEvent @event)
	{
		if (@event is InputEventMouseButton button && button.ButtonIndex == MouseButton.Left && button.Pressed)
		{
			talkIndex++;
			if (talkIndex < talkWords.Count)
			{
				UpdateStory();
			}
			else
			{
				talkIndex = 0;
				canvasLayer.Hide();
				EmitSignal(SignalName.OnTalkEnd);
			}
		}
	}

	public override void _Ready()
	{
		GetChildNodes();
		canvasLayer.Hide();
	}

	public void StartPlay(List<TalkModel> words)
	{
		talkWords = words;
		canvasLayer.Show();
		UpdateStory();
	}

	void UpdateStory()
	{
		talkStory = talkWords[talkIndex];
		actorAvatar.Texture = GameUtil.GetPilotAvatar(talkStory.Avatar);
		actorName.Text = talkStory.ActorName;
		actorWord.Text = talkStory.Word;
	}
}
